package com.alex.statistics.pojo;

import lombok.Data;

/**
 * 返回结果
 *
 */
@Data
public class Result<T> {
    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 成功,不带数据
     */
    public static <S> Result<S> ok() {
        Result<S> result = new Result<>();
        result.setCode(CodeEnum.ok.getCode());
        result.setMsg(CodeEnum.ok.getMsg());
        result.setData(null);

        return result;
    }

    /**
     * 成功,带数据
     */
    public static <S> Result<S> ok(S data) {
        Result<S> result = new Result<>();
        result.setCode(CodeEnum.ok.getCode());
        result.setMsg(CodeEnum.ok.getMsg());
        result.setData(data);

        return result;
    }

    /**
     * 失败,不带数据
     */
    public static <S> Result<S> fail() {
        Result<S> result = new Result<>();
        result.setCode(CodeEnum.fail.getCode());
        result.setMsg(CodeEnum.fail.getMsg());
        result.setData(null);

        return result;
    }

    /**
     * 失败,带数据
     */
    public static <S> Result<S> fail(S data) {
        Result<S> result = new Result<>();
        result.setCode(CodeEnum.fail.getCode());
        result.setMsg(CodeEnum.fail.getMsg());
        result.setData(data);

        return result;
    }

    /**
     * 返回指定的全局状态码,不带数据
     */
    public static <S> Result<S> msg(CodeEnum codeEnum) {
        Result<S> result = new Result<>();
        result.setCode(codeEnum.getCode());
        result.setMsg(codeEnum.getMsg());
        result.setData(null);

        return result;
    }

    /**
     * 返回指定的全局状态码,带数据
     */
    public static <S> Result<S> msg(CodeEnum codeEnum, S data) {
        Result<S> result = new Result<>();
        result.setCode(codeEnum.getCode());
        result.setMsg(codeEnum.getMsg());
        result.setData(data);

        return result;
    }
}
